package kr.co.graviti.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.graviti.common.util.CommonUtil;
import kr.co.graviti.service.cd.CdService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class FiledownLoad {
	
	@Value("${common.default.upload.path}") private String downloadDefaultPath;
	
	@Autowired
	private CdService commService;
	
	/** 첨부파일 다운로드 
	 * @param request
	 * @param response
	 * @param filepath
	 * @param fileseq
	 * @param filesirial
	 * @throws Exception
	 */
	@RequestMapping(value="/include/filedownload")
	public void adminfiledoload(HttpServletRequest request 
			,HttpServletResponse response
			,@RequestParam(value="filepath", required=true, defaultValue="") String filepath
			,@RequestParam(value="fileseq", required=true, defaultValue="0") int fileseq
			,@RequestParam(value="filesirial", required=true, defaultValue="0") int filesirial) throws Exception{
			
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("ATTCH_FILE_INFO_IDX", fileseq);
		param.put("ATTCH_FILE_IDX",filesirial);
		
		List<HashMap<String, Object>> fileList = commService.fileselect(param);		
		String fileSaveNm	= fileList.get(0).get("CHG_FILENM").toString();
		String fileOrgNm	= fileList.get(0).get("ORG_FILENM").toString();
		String path			= downloadDefaultPath + filepath + "/";
		 
		response.setHeader("Content-Disposition", "attachment; filename=\"" + CommonUtil.toEng(fileOrgNm) + "\";");
		response.setHeader("Content-Transfer-Encoding", "UTF-8");
		OutputStream out = response.getOutputStream();
		FileInputStream fis = null;
		
		File f = new File(path+fileSaveNm);		
		try {  
            fis = new FileInputStream(f);
            FileCopyUtils.copy(fis, out);
        } catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fis != null){
                try{
                    fis.close();
                }catch(Exception e){}
            }       
        }// try end;
        out.flush();	
	}
	
	@RequestMapping(value="/include/data_download")
	public void dataDownload(HttpServletRequest request 
			,HttpServletResponse response
			,@RequestParam(value="filename", required=true, defaultValue="") String filename) throws Exception{
			
		HashMap<String, Object> param = new HashMap<String, Object>();
		
		List<HashMap<String, Object>> fileList = commService.fileselect(param);		
		String path			= new StringBuffer(downloadDefaultPath).append("/data/").toString();
		
		response.setHeader("Content-Disposition", "attachment; filename=\"" + CommonUtil.toEng(filename) + "\";");
		response.setHeader("Content-Transfer-Encoding", "UTF-8");
		OutputStream out = response.getOutputStream();
		FileInputStream fis = null;
		
		File f = new File(path+filename);		
		try {  
            fis = new FileInputStream(f);
            FileCopyUtils.copy(fis, out);
        } catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fis != null){
                try{
                    fis.close();
                }catch(Exception e){}
            }       
        }// try end;
        out.flush();	
	}		
}
